//Andrew Villadsen

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


//Class UserKeyboardInputListener monitors keyboard input from the user for movement commands.
public class UserKeyboardInputListener extends KeyAdapter
{
	//Encodes which direction keys are currently being pressed.
	//The most significant nybble is not used. The bits of the least significant nybble are, from most significant to least: Up, W; Right, D; [none]; Left, A.
	private byte directionFlags = 0x00;
	
	//Sets the flag to represent the key being pressed.
	public void keyPressed(KeyEvent e)
	{
		switch(e.getKeyCode())
		{
		/**Direction Keys**/
		case KeyEvent.VK_UP:
		case KeyEvent.VK_W:
			directionFlags |= 0x08;
		break;
		
		case KeyEvent.VK_RIGHT:
		case KeyEvent.VK_D:
			directionFlags |= 0x04;
		break;
		
		case KeyEvent.VK_LEFT:
		case KeyEvent.VK_A:
			directionFlags |= 0x01;
		break;
		/**End Direction Keys**/
		
		default: break; //Do nothing if other key.
		}
	}
	
	//Sets the flag to no longer represent the key being released.
	public void keyReleased(KeyEvent e)
	{
		switch(e.getKeyCode())
		{
		/**Direction Keys**/
		case KeyEvent.VK_UP:
		case KeyEvent.VK_W:
			directionFlags &= 0x07;
		break;
		
		case KeyEvent.VK_RIGHT:
		case KeyEvent.VK_D:
			directionFlags &= 0x0B;
		break;
		
		case KeyEvent.VK_LEFT:
		case KeyEvent.VK_A:
			directionFlags &= 0x0E;
		break;
		/**End Direction Keys**/
		
		default: break; //Do nothing if other key.
		}
	}
	
	//Returns true if the player is receiving command to move forward.
	public boolean playerIsMoving()
	{
		return (directionFlags & 0x08) != 0x00;
	}
	
	//Returns 1 if the user is indicating a right turn; -1, a left turn; and otherwise, 0.
	public int getTurnDirection()
	{
		if(((directionFlags >> 2) & 0x01) > 0)
			return 1;
		if((directionFlags & 0x01) > 0)
			return -1;
		return 0;
	}
}